﻿using DAL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    
    public class PurchaseBillDAL
    {
        private SqlCommand cmd;
        public PurchaseBillDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }
        /// <summary>
        /// 加载订单存储过程参数列表
        /// </summary>
        private void SetPurchaseBillParameter(PurchaseBill bo)
        {
            cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;
            cmd.Parameters.Add("SupplierID", SqlDbType.NVarChar, 6).Value = bo.SupplierID;
            cmd.Parameters.Add("PurchaseDate", SqlDbType.DateTime).Value = bo.PurchaseDate;
            cmd.Parameters.Add("Clerk", SqlDbType.NVarChar, 6).Value = bo.Clerk;
            cmd.Parameters.Add("Onprocess", SqlDbType.SmallInt).Value = bo.OnProcess;
            cmd.Parameters.Add("Memo", SqlDbType.NVarChar,100).Value = bo.Memo;
        }
        /// <summary>
        /// 加载详单存储过程参数列表
        /// </summary>
        private void SetDetailParameter(PurchaseDetail bo)
        {
            cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseID;
            cmd.Parameters.Add("PurchaseDatailID", SqlDbType.SmallInt).Value = bo.PurchaseDetailID;
            cmd.Parameters.Add("ProductID", SqlDbType.NVarChar, 6).Value = bo.ProductID;
            cmd.Parameters.Add("PurchasePrice", SqlDbType.Decimal).Value = bo.PurchasePrice;
            cmd.Parameters.Add("Quantity", SqlDbType.Int).Value = bo.Quantity;
        }
        /// <summary>
        /// 订单修改方法实现（新增、修改、删除）
        /// </summary>
        /// <param name="bo">订单</param>
        /// <param name="opera">操作类型（Insert、Update、Delete）</param>
        /// <returns></returns>
        public bool Edit(PurchaseBill bo,string opera)
        {
            string operationString1 = opera + "PurchaseBill";//操作订单数据（Insert、Update）
            string operationString2 = opera + "PurchaseBillDetail";//操作详单数据（Insert、Update）

            SqlTransaction sqltran = SQLHelper.ExcuteSqlTransaction(cmd);//定义事务处理对象

            if (opera == "INSERT" || opera == "UPDATE")
            {
                cmd.CommandText = operationString1;
                cmd.Parameters.Clear();
                SetPurchaseBillParameter(bo);
                try
                {
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        foreach (PurchaseDetail item in bo.PurchaseDetailList)
                        {
                            cmd.CommandText = operationString2;
                            cmd.Parameters.Clear();
                            SetDetailParameter(item);
                            cmd.ExecuteNonQuery();
                        }
                        sqltran.Commit();//提交事务
                        return true;
                    }
                    else
                        return false;
                }
                catch //(Exception ex)
                {
                    sqltran.Rollback();//回滚事务
                    return false;
                }
                finally
                {
                    SQLHelper.CloseConn();//关闭数据库连接
                }
            }
            else
            {
                //删除订单实现
                try
                {
                    foreach(PurchaseDetail item in bo.PurchaseDetailList)
                    {
                        cmd.CommandText = operationString2;
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = item.PurchaseID;
                        cmd.Parameters.Add("PurchaseDatailID", SqlDbType.Int).Value = item.PurchaseDetailID;
                        cmd.ExecuteNonQuery();
                    }
                    cmd.CommandText = operationString1;
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;
                    cmd.Parameters.Add("Memo", SqlDbType.NVarChar,100).Value = bo.Memo;
                    cmd.ExecuteNonQuery();
                    sqltran.Commit(); //提交事务
                    return true;
                }
                catch//(Exception ex)
                {
                    sqltran.Rollback();//回滚事务
                    return false;
                }
                finally
                {
                    SQLHelper.CloseConn();//关闭数据库连接
                }
            }
        }
    }
}
